package com.ruoyi.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.WebsocketConst;
import com.ruoyi.common.core.domain.dto.MessageDTO;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.service.CommonService;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.websocket.WebSocketServer;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.domain.SysNoticeSend;
import com.ruoyi.system.mapper.SysNoticeMapper;
import com.ruoyi.system.mapper.SysNoticeSendMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import java.util.Date;
import javax.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@RequiredArgsConstructor
@Service
public class SysCommServiceImple implements CommonService {

    @Value("${flowable.message-base-url}")
    private String msgBaseUrl;

    @Resource
    private SysUserMapper userMapper;
    @Resource
    SysNoticeMapper sysNoticeMapper;
    @Resource
    private WebSocketServer webSocket;
    @Resource
    private SysNoticeSendMapper sysNoticeSendMapper;

    @Override
    public void sendSysNotice(MessageDTO message) throws JsonProcessingException {
        this.sendSysNotice(message.getFromUser(),
            message.getToUser(),
            message.getTitle(),
            message.getContent(),
            message.getCategory());
    }

    /**
     * 发消息
     * @param fromUser
     * @param toUser
     * @param title
     * @param msgContent
     * @param setMsgCategory
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sendSysNotice(Long fromUser, String toUser, String title, String msgContent, String setMsgCategory)
        throws JsonProcessingException {
        SysNotice notice = new SysNotice();
        notice.setNoticeTitle(title);
        notice.setNoticeContent(msgContent);
        notice.setNoticeType(setMsgCategory);
        notice.setSender(fromUser);
        notice.setPriority(Constants.PRIORITY_M);
        notice.setMsgType(Constants.MSG_TYPE_UESR);
        notice.setSendStatus(Constants.HAS_SEND);
        notice.setSendTime(new Date());
        notice.setMsgType(Constants.MSG_TYPE_UESR);
        notice.setStatus("0");
        sysNoticeMapper.insert(notice);
        // 2.插入用户通告阅读标记表记录
        String userId = toUser;
        String[] userIds = userId.split(",");
        Long noticeId = notice.getNoticeId();
        for(int i=0;i<userIds.length;i++) {
            if(ObjectUtil.isNotEmpty(userIds[i])) {
                SysUser sysUser = userMapper.selectUserById(Long.valueOf(userIds[i]));
                if(sysUser==null) {
                    continue;
                }
                SysNoticeSend noticeSend = new SysNoticeSend();
                noticeSend.setNoticeId(noticeId);
                noticeSend.setUserId(sysUser.getUserId());
                noticeSend.setReadFlag(Constants.NO_READ_FLAG);
                sysNoticeSendMapper.insert(noticeSend);
                JSONObject obj = new JSONObject();
                obj.put(WebsocketConst.MSG_CMD, WebsocketConst.CMD_USER);
                obj.put(WebsocketConst.MSG_USER_ID, sysUser.getUserName());
                obj.put(WebsocketConst.MSG_ID, notice.getNoticeId());
                obj.put(WebsocketConst.MSG_TXT, notice.getNoticeTitle());
                webSocket.sendMessage(sysUser.getUserName(), obj.toJSONString());
            }
        }

    }

    @Override
    public String getBaseUrl() {
        return msgBaseUrl;
    }

    @Override
    public LoginUser getLoginUser() {
        LoginUser user = LoginHelper.getLoginUser();
        return user;
    }
}
